this.BX=this.BX||{};this.BX.UI=this.BX.UI||{};(function(e,t,i){"use strict";const s=t.Reflection.namespace("BX.UI.Viewer.Item");const n=t.Reflection.namespace("BX.util");const r=t.Reflection.namespace("BX.Promise");const o=1.4;const a=3;var l=babelHelpers.classPrivateFieldLooseKey("loadingLibraryPromise");var h=babelHelpers.classPrivateFieldLooseKey("pageNumber");var c=babelHelpers.classPrivateFieldLooseKey("loadingDocumentPromise");var d=babelHelpers.classPrivateFieldLooseKey("resetState");class u extends s{constructor(e){super(e);Object.defineProperty(this,d,{value:g});Object.defineProperty(this,h,{writable:true,value:1});Object.defineProperty(this,c,{writable:true,value:null});this.pdfPages={};this.scale=o;this.pdfRenderedPages={};this.lastRenderedPdfPage=0;this.disableAnnotationLayer=false;e=e||{};this.scale=e.scale||o}setPropertiesByNode(e){super.setPropertiesByNode(e);this.disableAnnotationLayer=e.dataset.hasOwnProperty("disableAnnotationLayer")}applyReloadOptions(e){this.controller.unsetCachedData(this.src)}listContainerModifiers(){const e=["ui-viewer-document"];if(this.controller.stretch){e.push("--stretch")}return e}setSrc(e){this.src=e;this._pdfSrc=null;return babelHelpers.classPrivateFieldLooseBase(this,d)[d]()}setPdfSource(e){this._pdfSrc=e;return babelHelpers.classPrivateFieldLooseBase(this,d)[d]()}loadLibrary(){if(babelHelpers.classPrivateFieldLooseBase(u,l)[l]!==null){return babelHelpers.classPrivateFieldLooseBase(u,l)[l]}babelHelpers.classPrivateFieldLooseBase(u,l)[l]=new Promise(((e,i)=>{t.Runtime.loadExtension("ui.pdfjs").then((()=>{if(!pdfjsLib.GlobalWorkerOptions.workerSrc){pdfjsLib.GlobalWorkerOptions.workerSrc="/bitrix/js/ui/pdfjs/pdf.worker.js"}babelHelpers.classPrivateFieldLooseBase(u,l)[l]=null;e()})).catch(i)}));return babelHelpers.classPrivateFieldLooseBase(u,l)[l]}loadData(){const e=new r;if(this._pdfSrc){this.loadLibrary().then((()=>{e.fulfill(this)}));return e}console.log("loadData pdf");const i=t.ajax.promise({url:t.Uri.addParam(this.src,{ts:"bxviewer"}),method:"GET",dataType:"json",headers:[{name:"BX-Viewer-src",value:this.src},{name:"BX-Viewer",value:"document"}]});i.then((i=>{if(!i||!i.data){this.isTransforming=false;e.reject({item:this,message:t.Loc.getMessage("JS_UI_VIEWER_ITEM_TRANSFORMATION_ERROR_1").replace("#DOWNLOAD_LINK#",this.getSrc()),type:"error"});return e}if(i.data.hasOwnProperty("pullTag")){if(!this.isTransforming){this.transformationPromise=e;this.registerTransformationHandler(i.data.pullTag)}this.isTransforming=true}if(i.data.data&&i.data.data.src){this.isTransforming=false;this._pdfSrc=i.data.data.src;this.loadLibrary().then((()=>{e.fulfill(this)}))}}));return e}render(){this.controller.showLoading();this.contentNode=t.Dom.create("div",{props:{className:"ui-viewer-item-document-content",tabIndex:2208}});t.Event.bind(this.contentNode,"scroll",t.Runtime.throttle(this.handleScrollDocument.bind(this),100));return this.contentNode}getNakedActions(){const e=super.getNakedActions();return this.insertPrintBeforeInfo(e)}insertPrintBeforeInfo(e){e=e||[];let t=null;for(let i=0;i<e.length;i++){if(e[i].type==="info"){t=i}}const i={type:"print",action:this.print.bind(this)};if(t===null){e.push(i)}else{e=n.insertIntoArray(e,t,i)}return e}getFirstDocumentPageHeight(){if(this._height){return Promise.resolve(this._height)}return new Promise((e=>{this.getDocumentPage(this.pdfDocument,1).then((t=>{const i=t.getViewport(this.scale);this._height=i.height;e(this._height)}))}))}handleScrollDocument(e){this.getFirstDocumentPageHeight().then((e=>{const t=this.contentNode.scrollHeight-this.contentNode.scrollTop-this.contentNode.clientHeight;if(t<e*a&&this.pdfDocument.numPages>this.lastRenderedPdfPage){for(let e=this.lastRenderedPdfPage+1;e<=Math.min(this.pdfDocument.numPages,this.lastRenderedPdfPage+a);e++){this.renderDocumentPage(this.pdfDocument,e)}}this.setPageNumber(this.contentNode.scrollTop/e+1)}))}loadDocument(){if(this.pdfDocument){return Promise.resolve(this.pdfDocument)}if(babelHelpers.classPrivateFieldLooseBase(this,c)[c]){return babelHelpers.classPrivateFieldLooseBase(this,c)[c]}babelHelpers.classPrivateFieldLooseBase(this,c)[c]=new Promise((e=>{this.loadData().then((()=>{pdfjsLib.getDocument(this._pdfSrc).promise.then((t=>{this.pdfDocument=t;babelHelpers.classPrivateFieldLooseBase(this,c)[c]=null;e(this.pdfDocument)}))}))}));return babelHelpers.classPrivateFieldLooseBase(this,c)[c]}getDocumentPage(e,t){if(this.pdfPages[t]){return Promise.resolve(this.pdfPages[t])}return new Promise((i=>{e.getPage(t).then((e=>{this.pdfPages[t]=e;i(this.pdfPages[t])}))}))}renderDocumentPage(e,i){const s=this.pdfRenderedPages[i];if(s instanceof Promise){return s}else if(!!s){return Promise.resolve(s)}this.pdfRenderedPages[i]=new Promise((s=>{this.getDocumentPage(e,i).then((e=>{const n=this.createCanvasPage();const r=e.getViewport(this.scale);n.height=r.height;n.width=r.width;const o=e.render({canvasContext:n.getContext("2d"),viewport:r});if(!this.disableAnnotationLayer){o.then((function(){return e.getAnnotations()})).then((function(i){const s=t.Dom.create("div",{props:{className:"ui-viewer-pdf-annotation-layer"}});t.Dom.insertAfter(s,n);t.Dom.adjust(s,{style:{margin:"-"+n.offsetHeight+"px auto 0 auto",height:n.height+"px",width:n.width+"px"}});pdfjsLib.AnnotationLayer.render({viewport:r.clone({dontFlip:true}),linkService:pdfjsLib.SimpleLinkService,div:s,annotations:i,page:e})}))}o.then((function(){return e.getTextContent()})).then((function(e){const i=t.Dom.create("div",{props:{className:"ui-viewer-pdf-text-layer"}});t.Dom.insertAfter(i,n);t.Dom.adjust(i,{style:{margin:"-"+n.offsetHeight+"px auto 0 auto",height:n.height+"px",width:n.width+"px"}});pdfjsLib.renderTextLayer({textContent:e,container:i,viewport:r,textDivs:[]})}));this.lastRenderedPdfPage=Math.max(i,this.lastRenderedPdfPage);if(i===1){this.firstWidthDocumentPage=n.width}o.then((()=>{this.controller.hideLoading();this.pdfRenderedPages[i]=e;s(e,n)}))}))}));return this.pdfRenderedPages[i]}createCanvasPage(){const e=document.createElement("canvas");e.className="ui-viewer-document-page-canvas";this.contentNode.appendChild(e);return e}getContentWidth(){if(this.firstWidthDocumentPage){return Promise.resolve(this.firstWidthDocumentPage)}return new Promise((e=>{this.loadDocument().then((()=>{this.renderDocumentPage(this.pdfDocument,1).then((t=>{e(t.getViewport(this.scale).width)}))}))}))}afterRender(){this.loadDocument().then((e=>{for(let i=1;i<=Math.min(e.numPages,a);i++){if(i===1){this._handleControls=this.controller.handleVisibleControls.bind(this.controller);this.controller.enableReadingMode(true);const e=this.controller.actionPanel.getItemById("print");if(e){e.layout.container.classList.remove("ui-btn-disabled")}t.Runtime.throttle(t.Event.bind(window,"mousemove",this._handleControls),20)}this.renderDocumentPage(e,i)}}))}beforeHide(){this.pdfRenderedPages={};t.Event.unbind(window,"mousemove",this._handleControls);if(this.printer){this.hidePrintProgress();this.printer.destroy()}}updatePrintProgressMessage(e,i){const s=Math.round(e/i*100);this.controller.setTextOnLoading(t.Loc.getMessage("JS_UI_VIEWER_ITEM_PREPARING_TO_PRINT").replace("#PROGRESS#",s))}showPrintProgress(e,t){this.contentNode.style.opacity=.7;this.contentNode.style.filter="blur(2px)";this.controller.showLoading({zIndex:1});this.updatePrintProgressMessage(e,t)}hidePrintProgress(){this.contentNode.style.opacity=null;this.contentNode.style.filter=null;this.controller.hideLoading()}print(){if(!this.pdfDocument){console.warn("Where is pdf document to print?");return}this.showPrintProgress(0,this.pdfDocument.numPages);this.printer=new p({pdf:this.pdfDocument});this.printer.init().then((()=>{this.printer.prepare({onProgress:this.updatePrintProgressMessage.bind(this)}).then((()=>{this.hidePrintProgress();this.printer.performPrint()}))}))}handleKeyPress(e){switch(e.code){case"PageDown":case"PageUp":case"ArrowDown":case"ArrowUp":BX.focus(this.contentNode);break}}getScale(){return this.scale}setScale(e){this.scale=e;return this}updateScale(e){e=Number(e);if(this.scale===e){return Promise.resolve()}const i=e/this.scale;const s=(e,i,s)=>{const n=i[e.pageIndex];if(!n){return Promise.resolve()}return new Promise((i=>{const r=e.getViewport(this.scale);n.width=r.width;n.height=r.height;e.render({canvasContext:n.getContext("2d"),viewport:r}).then((()=>{const o=s[e.pageIndex];if(o){t.Dom.clean(o);t.Dom.adjust(o,{style:{margin:"-"+n.offsetHeight+"px auto 0 auto",height:r.height+"px",width:r.width+"px"}});e.getTextContent().then((e=>{pdfjsLib.renderTextLayer({textContent:e,container:o,viewport:r,textDivs:[]});i()}))}else{i()}}))}))};const n=[];this.scale=e;const r=Array.from(this.contentNode.querySelectorAll('canvas[class="ui-viewer-document-page-canvas"]'));const o=Array.from(this.contentNode.querySelectorAll('div[class="ui-viewer-pdf-text-layer"]'));Object.values(this.pdfRenderedPages).forEach((e=>{if(e instanceof Promise){n.push(new Promise((t=>{e.then((e=>{s(e,r,o).then(t)}))})))}else{n.push(s(e,r,o))}}));const a=this.contentNode.scrollTop*i;this.contentNode.scrollTo(this.contentNode.scrollLeft,a);return Promise.all(n)}getPagesNumber(){if(!this.pdfDocument){return null}return t.Text.toInteger(this.pdfDocument._pdfInfo.numPages)}scrollToPage(e){const t=this.setPageNumber(e)!==null;if(!t){return Promise.resolve()}return new Promise((t=>{const i=[];for(let t=1;t<e;t++){i.push(this.renderDocumentPage(this.pdfDocument,t))}Promise.all(i).then((e=>{let i=0;e.forEach((e=>{const t=e.getViewport(this.scale);i+=t.height+7}));this.contentNode.scrollTo(this.contentNode.scrollLeft,i);t()}))}))}getPageNumber(){return babelHelpers.classPrivateFieldLooseBase(this,h)[h]}setPageNumber(e){e=t.Text.toInteger(e);if(e<0){e=1}let s=this.getPagesNumber();if(!s){s=1}if(e>s){e=s}if(babelHelpers.classPrivateFieldLooseBase(this,h)[h]!==e){babelHelpers.classPrivateFieldLooseBase(this,h)[h]=e;i.EventEmitter.emit(this,"BX.UI.Viewer.Item.Document:updatePageNumber");return this}return null}}function g(){this.pdfRenderedPages={};this.lastRenderedPdfPage=null;this.pdfDocument=null;this.pdfPages={};this.setPageNumber(1);if(this.printer){this.hidePrintProgress();this.printer.destroy()}}Object.defineProperty(u,l,{writable:true,value:null});const m=1;class p{constructor(e){e=e||{};this.pdf=e.pdf;this.iframe=null;this.documentOverview={}}init(){if(this.documentOverview){return Promise.resolve(this.documentOverview)}return new Promise((e=>{this.pdf.getPage(1).then((t=>{const i=t.getViewport(m);this.documentOverview={width:i.width,height:i.height,rotation:i.rotation};e(this.documentOverview)}))}))}prepare(e){e=e||{};const i=this.pdf.numPages;let s=-1;const n=new r;let o=null;if(t.Type.isFunction(e.onProgress)){o=e.onProgress}this.frame=this.createIframe();const a=()=>{if(++s>=i){console.log("finish",this.frame.contentWindow.document);setTimeout((()=>{n.fulfill()}),1e3);return}this.renderPage(s+1).then((function(){if(o){o(s+1,i)}a()}))};a();return n}renderPage(e){return this.pdf.getPage(e).then((function(e){const t=document.createElement("canvas");const i=e.getViewport(1);const s=150;const n=s/72;t.width=Math.floor(i.width*n);t.height=Math.floor(i.height*n);const r=96/72;const o=Math.floor(i.width*r)+"px";const a=Math.floor(i.height*r)+"px";const l=t.getContext("2d");l.save();l.fillStyle="rgb(255, 255, 255)";l.fillRect(0,0,t.width,t.height);l.restore();const h={canvasContext:l,transform:[n,0,0,n,0,0],viewport:e.getViewport(1,i.rotation),intent:"print"};return e.render(h).promise.then((function(){return{scratchCanvas:t,width:o,height:a}}))})).then((e=>{const t=document.createElement("img");t.style.width=e.width;t.style.height=e.height;const i=e.scratchCanvas;if("toBlob"in i&&!this.disableCreateObjectURL){i.toBlob((function(e){t.src=URL.createObjectURL(e)}))}else{t.src=i.toDataURL()}const s=document.createElement("div");s.appendChild(t);this.frame.contentWindow.document.body.appendChild(s)}))}destroy(){if(this.frame){t.Dom.remove(this.frame)}}createIframe(){const e=document.createElement("iframe");e.src="about:blank";e.name="document-print-frame";e.style.display="none";document.body.appendChild(e);const t=e.contentWindow;const i=t.document;i.open();i.write("<html><head>");const s=this.getDocumentOverview();let n="<style>";n+="html, body { background: #fff !important; height: 100%; }";n+="@supports ((size:A4) and (size:1pt 1pt)) {"+"@page { size: "+s.width+"pt "+s.height+"pt;}"+"}";n+="#ad{ display:none;}";n+="#leftbar{ display:none;}";n+="</style>";i.write(n);i.write("</head><body>");i.write("</body></html>");i.close();return e}performPrint(){this.frame.contentWindow.focus();this.frame.contentWindow.print()}getDocumentOverview(){return this.documentOverview}}let f=e=>e,P,v,w,b,C,D,L,y,I;const N=t.Reflection.namespace("BX.UI.Viewer.InlineController");class _ extends N{bindEvents(){if(!this.eventsAlreadyBinded&&this.getDocumentItem()){i.EventEmitter.subscribe(this.getDocumentItem(),"BX.UI.Viewer.Item.Document:updatePageNumber",(()=>{this.getListingControl().update(this.getDocumentItem().getPageNumber())}))}super.bindEvents()}getDocumentItem(){return this.items[0]}updateControls(){super.updateControls();this.updateListingControl()}getViewerContainer(){if(!this.layout.container){this.layout.inner=t.Tag.render(P||(P=f`<div class="ui-viewer__single-document--container ">${0}</div>`),this.getItemContainer());if(this.stretch){t.Dom.addClass(this.layout.inner,"--stretch")}this.layout.container=t.Tag.render(v||(v=f`<div class="">${0}${0}</div>`),this.layout.inner,this.getControlsContainer())}return this.layout.container}getControlsContainer(){if(!this.layout.controlsContainer){return t.Tag.render(w||(w=f`<div class="ui-viewer__single-document--controls">
				${0}
				${0}
			</div>`),this.getListingControl().render(),this.getScaleControl().render())}return this.layout.controlsContainer}getListingControl(){if(!this.listingControl){this.listingControl=new S;this.listingControl.subscribe("pageUpdated",(()=>{var e;(e=this.getDocumentItem())==null?void 0:e.scrollToPage(this.listingControl.getCurrent())}));this.updateListingControl()}return this.listingControl}getScaleControl(){if(!this.scaleControl){this.scaleControl=new E;this.scaleControl.subscribe("scaleUpdated",(()=>{var e;(e=this.getDocumentItem())==null?void 0:e.updateScale(this.scaleControl.getScale())}))}return this.scaleControl}updateListingControl(){const e=this.getDocumentItem();if(e){e.loadDocument().then((()=>{this.listingControl.update(1,e.getPagesNumber())}))}}setScale(e){var t;(t=this.getDocumentItem())==null?void 0:t.setScale(e);this.getScaleControl().update(e);return this}setPdfSource(e){var t;(t=this.getDocumentItem())==null?void 0:t.setPdfSource(e);return this}print(){var e;(e=this.getDocumentItem())==null?void 0:e.print()}}class S extends i.EventEmitter{constructor(e=1,i=1){super();this.container=null;this.pagesContainer=null;this.setEventNamespace("BX.UI.Viewer.SingleDocumentController.ListingControl");this.pages=t.Text.toInteger(i);this.current=t.Text.toInteger(e);this.arrowClickHandler=this.handleArrowClick.bind(this)}update(e,i=null){e=t.Text.toInteger(e);i=t.Text.toInteger(i);if(i>=1){this.pages=i}if(e<1){e=1}if(e>this.pages){e=this.pages}if(e!==this.current){this.current=e;this.emit("pageUpdated",{page:this.current})}this.adjust()}adjust(){this.pagesContainer.innerHTML=this.renderPages()}getCurrent(){return this.current}render(){if(!this.container){this.pagesContainer=t.Tag.render(b||(b=f`<div class="ui-viewer__single-document--listing-info">
				${0}
			</div>`),this.renderPages());this.container=t.Tag.render(C||(C=f`<div class="ui-viewer__single-document--listing">
				<div class="ui-viewer__single-document--listing--btn --prev" onclick="${0}"></div>
				${0}
				<div class="ui-viewer__single-document--listing--btn --next" onclick="${0}"></div>
			</div>`),this.arrowClickHandler,this.pagesContainer,this.arrowClickHandler)}return this.container}renderPages(){return t.Loc.getMessage("JS_UI_VIEWER_SINGLE_DOCUMENT_LISTING_PAGES").replace("#CURRENT#",this.current).replace("#ALL#",this.pages)}handleArrowClick(e){if(e.target.classList.contains("--prev")){this.update(this.current-1)}if(e.target.classList.contains("--next")){this.update(this.current+1)}}}const T=.5;const x=3;class E extends i.EventEmitter{constructor(){super();this.scale=T;this.container=null;this.zoomInContainer=null;this.zoomOutContainer=null;this.zoomValueNode=null;this.scale=T;this.setEventNamespace("BX.UI.Viewer.SingleDocumentController.ScaleControl");this.scaleClickHandler=this.handleScaleClick.bind(this)}getScale(){return this.scale}setDefaultScale(){this.update(T)}adjust(){if(this.scale<=T){t.Dom.hide(this.getZoomOutContainer())}else{t.Dom.show(this.getZoomOutContainer())}if(this.scale>=x){t.Dom.hide(this.getZoomInContainer())}else{t.Dom.show(this.getZoomInContainer())}this.getZoomValueNode().innerText=Math.round(this.scale*100)}update(e){e=t.Text.toNumber(e);if(e<T){e=T}if(e>x){e=x}if(e!==this.scale){this.scale=e;this.emit("scaleUpdated");this.adjust()}}render(){if(!this.container){this.container=t.Tag.render(D||(D=f`<div class="ui-viewer__single-document--zoom">
				${0}
				${0}
				${0}
			</div>`),this.getZoomOutContainer(),this.getZoomValueNode(),this.getZoomInContainer());this.adjust()}return this.container}getZoomInContainer(){if(!this.zoomInContainer){this.zoomInContainer=t.Tag.render(L||(L=f`<div
				class="ui-viewer__single-document--zoom-control --zoom-in"
				onclick="${0}"
			>
<!--				${0}-->
			</div>`),this.scaleClickHandler,t.Loc.getMessage("JS_UI_VIEWER_SINGLE_DOCUMENT_SCALE_ZOOM_IN"))}return this.zoomInContainer}getZoomOutContainer(){if(!this.zoomOutContainer){this.zoomOutContainer=t.Tag.render(y||(y=f`<div 
				class="ui-viewer__single-document--zoom-control --zoom-out"
				onclick="${0}"
			>
<!--				${0}-->
			</div>`),this.scaleClickHandler,t.Loc.getMessage("JS_UI_VIEWER_SINGLE_DOCUMENT_SCALE_ZOOM_OUT"))}return this.zoomOutContainer}getZoomValueNode(){if(!this.zoomValueNode){this.zoomValueNode=t.Tag.render(I||(I=f`<span class="ui-viewer__single-document--zoom-value">100</span>`))}return this.zoomValueNode}handleScaleClick(e){let t=this.scale;if(e.target.classList.contains("--zoom-in")){t=this.scale*1.1}if(e.target.classList.contains("--zoom-out")){t=this.scale*.9}this.update(t)}}e.Document=u;e.PrintService=p;e.SingleDocumentController=_})(this.BX.UI.Viewer=this.BX.UI.Viewer||{},BX,BX.Event);
//# sourceMappingURL=viewer.bundle.map.js